/**
 * @author mithun
 */

Event.observe(window, 'load', function(event) {
		
	//getParameterList();
	if (browser.isIE) {
		initIEAjaxObservers();
	}	

});

var getParameterList = function() {
	
	var configurationId = $F('ConfigurationId');
	new Ajax.Updater(
		'parameterList',
		WEBROOT + 'admin/configuration_parameters/index',
		{
			method         : 'get',
			asynchronous   : true,
			evalScripts    : true,
			parameters     : {
				'sortBy'           : 'configuration_parameter_name',
				'configuration_id' : configurationId
			},
			requestHeaders : ['X-Update', 'parameterList']
		}
	);
	
}

var newParameter = function(id) {

	if (browser.isIE) {
		// Ajax Responce may contains <form> tag, which will cause IE rendering failure.
		newParameterIE(id);
		return;
	}
	var configurationId = $F('ConfigurationId');
	new Ajax.Updater(
		'editParameter',
		WEBROOT + 'admin/configuration_parameters/add',
		{
			method         : 'get',
			asynchronous   : true,
			evalScripts    : true,
			parameters     : {
				'configuration_id': configurationId
			},
			requestHeaders : ['X-Update', 'editParameter'],
			onLoading      : ajaxLoading,
			onSuccess      : getParameterList
		}
	);
}

var editParameter = function(id) {
	
	if (browser.isIE) {
		// Ajax Responce may contains <form> tag, which will cause IE rendering failure.
		editParameterIE(id);
		return;
	}
	 
	new Ajax.Updater(
		'editParameter',
		WEBROOT + '/admin/configuration_parameters/edit/'+id,
		{
			asynchronous   : true,
			evalScripts    : true,
			requestHeaders : ['X-Update', 'editParameter'],
			onLoading      : ajaxLoading,
			onSuccess      : getParameterList
		}
	);
	
}

var deleteParameter = function(id) {
	
	if (confirm('Are you sure you want to delete this slot ?')) {
		new Ajax.Updater(
			'editParameter',
			WEBROOT + '/admin/configuration_parameters/delete/'+id,
			{
				method         : 'get',
				asynchronous   : true,
				evalScripts    : true, 
				requestHeaders : ['X-Update', 'editParameter'],
				onLoading      : ajaxLoading,
				onSuccess      : getParameterList 
			}
		);
	}
	
}
 
var initIEAjaxObservers = function() {
	
	Event.observe("newParameterButton", 'click', function(event) {
		var configurationId = $F('ConfigurationId'); 
		new Ajax.Updater(
			'editParameter',
			WEBROOT + '/admin/configuration_parameters/add?configuration_id='+configurationId,
			{
				method         : 'get',
				asynchronous   : true,
				evalScripts    : true,
				parameters     : Form.serialize(Event.element(event).form),
				requestHeaders : ['X-Update', 'editParameter'],
				onSuccess      : ieAjaxSuccessHandler, 
				//onSuccess    : function(t){ alert(t.responseText)},
				onFailure      : ieAjaxErrorHandler
			})
		},
	false);
			
}

var newParameterIE = function(id) {
	
	var configurationId = $F('ConfigurationId');
	new Ajax.Updater(
		'editParameter',
		WEBROOT + '/admin/configuration_parameters/add?configuration_id='+configurationId,
		{
			method         : 'get',
			asynchronous   : true,
			evalScripts    : true,
			parameters     : {
				'configuration_id': configurationId
			},
			requestHeaders :['X-Update', 'editParameter'],
			onLoading      : ajaxLoading,
			onSuccess      : ieAjaxSuccessHandler, 
			onFailure      : ieAjaxErrorHandler
		}
	);
}

var editParameterIE = function(id) {
	
	new Ajax.Updater(
		'editParameter',
		WEBROOT + '/admin/configuration_parameters/edit/'+id,
		{
			method         : 'get',
			asynchronous   : true,
			evalScripts    : true,
			requestHeaders : ['X-Update', 'editParameter'],
			onLoading      : ajaxLoading,
			onSuccess      : ieAjaxSuccessHandler, 
			onFailure      : ieAjaxErrorHandler
		}
	);

}

var ieAjaxSuccessHandler = function(t) {

	var oldCell = $('editParameter').parentNode; 
	var newCell = document.createElement("TD");
	var objRow  = oldCell.parentNode; 
	var newDiv = document.createElement("DIV");
	newDiv.setAttribute('id', 'editParameter');
	newDiv.innerHTML = t.responseText;
	newCell.appendChild(newDiv);
	objRow.removeChild(oldCell);
	objRow.appendChild(newCell);
	getParameterList();
	
	if ($("newParameterButton")) {
		Event.observe("newParameterButton", 'click', function(event) {
			var configurationId = $F('ConfigurationId'); 
			//alert("newParameterButton clicked");
			//alert(Form.serialize(Event.element(event).form));
			new Ajax.Updater(
				'editParameter',
				WEBROOT + 'admin/configuration_parameters/add?configuration_id='+configurationId,
				{
					method         : 'post',
					asynchronous   : true,
					evalScripts    : true,
					parameters     : Form.serialize(Event.element(event).form),
					requestHeaders : ['X-Update', 'editParameter'],
					onSuccess      : ieAjaxSuccessHandler, 
					//onSuccess    : function(t){ alert(t.responseText)},
					onFailure      : ieAjaxErrorHandler
				})
			},
		false);
	}	
	
	if ($("saveParameterButton")) {
		Event.observe("saveParameterButton", 'click', function(event) {
			var id = $F('ConfigurationParameterId');  
			new Ajax.Updater(
				'editParameter',
				WEBROOT + '/admin/configuration_parameters/edit/'+id,
				{
					method         : 'post',
					asynchronous   : true,
					evalScripts    : true,
					parameters     : Form.serialize(Event.element(event).form),
					requestHeaders : ['X-Update', 'editParameter'],
					onSuccess      : ieAjaxSuccessHandler, 
					onFailure      : ieAjaxErrorHandler
				})
			},
		false);	
	}
	
	if ($("cancelActionButton")) {
		Event.observe("cancelActionButton", 'click', function(event) {
			new Ajax.Updater(
				'editParameter',
				WEBROOT + '/admin/configuration_parameters/cancel',
				{
					method         : 'get',
					asynchronous   : true,
					evalScripts    : true,
					requestHeaders : ['X-Update', 'editParameter'],
					onSuccess      : ieAjaxSuccessHandler, 
					onFailure      : ieAjaxErrorHandler
				})
			},
		false);
	}	
	
}

var ieAjaxErrorHandler = function(t) {
	
    alert('Error ' + t.status + ' -- ' + t.statusText);
	
}

var ajaxLoading = function() {
	
	var htmlString = "<div id='ajaxLoaderContainer'><img src='"+WEBROOT+"img/ajax_loader_01.gif' alt = 'Loading...' /></div>";	
	$('editParameter').innerHTML = htmlString;
		
}
